package cate.game.common.rpc;

import cate.game.GameBody;
import cate.game.framework.GameException;
import cp.solution.rpc.template.RpcSenderTemplate;
import org.apache.commons.lang3.StringUtils;

public class RpcSender4Game {

    private GameBody game;
    private RpcSenderTemplate template;

    public RpcSender4Game(GameBody game, RpcSenderTemplate template) {
        this.game = game;
        this.template = template;
    }

    /** 向指定游戏服发起请求 */
    public <T> T requestResponseGame(String path, int srvId, Object input, Class<T> respClz) throws GameException {
        try {
            return this.template.requestResponseGameTemplate(path, srvId, input, respClz);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    /** 发送消息到指定游戏服 */
    public void fireAndForgetGame(String path, int srvId, Object input) throws GameException {
        try {
            this.template.fireAndForgetGameTemplate(path, srvId, input);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    /** 通知所有属于这个跨服组的游戏服 */
    public void fireAndForgetGameInCross(String path, String crossCode, Object input) throws GameException {
        try {
            this.template.fireAndForgetGameInCrossTemplate(path, crossCode, input);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    public void fireAndForgetGameInCross(String path, Object input) throws GameException {
        fireAndForgetGameInCross(path, game.config.gs.getCrossCode(), input);
    }

    /** 通知所有属于这个专服(组服)组的游戏服 */
    public void fireAndForgetGameInGroup(String path, String groupCode, Object input) throws GameException {
        try {
            this.template.fireAndForgetGameInGroupTemplate(path, groupCode, input);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    public void fireAndForgetGameInGroup(String path, Object input) throws GameException {
        fireAndForgetGameInGroup(path, game.config.gs.getGroupCode(), input);
    }

    // TO CROSS

    /** 发送到自身游戏服对应跨服，不需要手动指派跨服编码 */
    public <T> T requestResponseCross(String path, Object input, Class<T> respClz) throws GameException {
        String crossCode = game.config.gs.getCrossCode();
        if(StringUtils.isBlank(crossCode)) {
            throw new GameException().content("缺少对应的跨服编码");
        }
        try {
            return this.template.requestResponseCrossTemplate(path, crossCode, input, respClz);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    /** 发送到自身游戏服对应跨服，不需要手动指派跨服编码 */
    public void fireAndForgetCross(String path, Object input) throws GameException {
        String crossCode = game.config.gs.getCrossCode();
        if(StringUtils.isBlank(crossCode)) {
            throw new GameException().content("缺少对应的跨服编码");
        }
        try {
            this.template.fireAndForgetCrossTemplate(path, crossCode, input);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    // TO GROUP

    /** 发送到自身游戏服对应组服，不需要手动指派组服编码 */
    public <T> T requestResponseGroup(String path, Object input, Class<T> respClz) throws GameException {
        String groupCode = game.config.gs.getGroupCode();
        if(StringUtils.isBlank(groupCode)) {
            throw new GameException().content("缺少对应的组服编码");
        }
        try {
            return this.template.requestResponseGroupTemplate(path, groupCode, input, respClz);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    /** 发送到自身游戏服对应组服，不需要手动指派组服编码 */
    public void fireAndForgetGroup(String path, Object input) throws GameException {
        String groupCode = game.config.gs.getGroupCode();
        if(StringUtils.isBlank(groupCode)) {
            throw new GameException().content("缺少对应的组服编码");
        }
        try {
            this.template.fireAndForgetGroupTemplate(path, groupCode, input);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    //===================== Console =====================

    public <T> T requestResponseConsole(String path, Object input,Class<T> respClz) throws GameException {
        try {
            return this.template.console().requestResponseTemplate(path, input, respClz);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    public void fireAndForgetConsole(String path, Object input) throws GameException {
        try {
            this.template.console().fireAndForgetTemplate(path, input);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    //===================== World =====================

    public <T> T requestResponseWorld(String path, Object input,Class<T> respClz) throws GameException {
        try {
            return this.template.world().requestResponseTemplate(path, input, respClz);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    public void fireAndForgetWorld(String path, Object input) throws GameException {
        try {
            this.template.world().fireAndForgetTemplate(path, input);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    //===================== Stat =====================

    public <T> T requestResponseStat(String path, Object input,Class<T> respClz) throws GameException {
        try {
            return this.template.stat().requestResponseTemplate(path, input, respClz);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    public void fireAndForgetStat(String path, Object input) throws GameException {
        try {
            this.template.stat().fireAndForgetTemplate(path, input);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    //===================== BI =====================

    public <T> T requestResponseBi(String path, Object input,Class<T> respClz) throws GameException {
        try {
            return this.template.bi().requestResponseTemplate(path, input, respClz);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }

    public void fireAndForgetBi(String path, Object input) throws GameException {
        try {
            this.template.bi().fireAndForgetTemplate(path, input);
        } catch (Exception e) {
            throw new GameException(e);
        }
    }
}
